﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Amgine
{
    public partial class ResetUserPasswordForm : Form
    {
        public ResetUserPasswordForm()
        {
            InitializeComponent();
        }

        private void button_confirm_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox_old_password.Text))
            {
                MessageBox.Show("请输入旧密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (string.IsNullOrWhiteSpace(textBox_new_password.Text))
            {
                MessageBox.Show("请输入新密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (string.IsNullOrWhiteSpace(textBox_repeat_password.Text))
            {
                MessageBox.Show("请再次输入新密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (!Properties.Settings.Default.user_password.Equals(textBox_old_password.Text))
            {
                textBox_old_password.Text = "";
                MessageBox.Show("旧密码不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (textBox_new_password.Text.Length < 6 || textBox_new_password.Text.Length > 32)
            {
                MessageBox.Show("密码长度必须为6-32位", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (!Regex.IsMatch(textBox_new_password.Text, "^[a-zA-Z0-9]*$"))
            {
                MessageBox.Show("密码只能包含字母和数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (!textBox_new_password.Text.Equals(textBox_repeat_password.Text))
            {
                MessageBox.Show("两次输入的新密码不匹配", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                Properties.Settings.Default.user_password = textBox_new_password.Text;
                Properties.Settings.Default.Save();
                EncryptDecryptUtils.GLOBAL_INIT_VAR = EncryptDecryptUtils.prepareMD5ByteArray32(Properties.Settings.Default.user_password);
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void button_reset_Click(object sender, EventArgs e)
        {
            textBox_old_password.Text = "";
            textBox_new_password.Text = "";
            textBox_repeat_password.Text = "";
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}
